package com.greenant.store.activity;


import java.util.ArrayList;

import android.app.ActionBar;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.greenant.store.R;
import com.greenant.store.bean.Order;
import com.greenant.store.bean.OrderStatusCode;
import com.greenant.store.fragment.MyFragmentPagerAdapter;
import com.greenant.store.fragment.OrdersListFragment;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.lsarah.utils.SystemBarTintManager;

/**
 * * 
 *
 * 
 * 
 */
public class MyOrderActivity extends FragmentActivity implements OnRefreshListener<ListView> {
	public static final String PENDING_ORDER ="pending";//���
	public static final String WAIT_FOR_PAY_ORDER ="waitForPay";//���
	public static final String SHIPPED_ORDER ="shipped";//�ѷ���
	public static final String REFUND_ORDER ="refund";//�˿���
	public static final String FINISHED_ORDER ="finished";
	public static final String CLOSED_ORDER ="closed";
    private static final String TAG = "MyOrderActivity";
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentsList;
    private ImageView ivBottomLine;
    private TextView tvTab1, tvTab2, tvTab3;
    private TextView tvSubTab1, tvSubTab2, tvSubTab3,tvSubTab4;

    private int currIndex = 0;
    private int bottomLineWidth;
    private int offset = 0;
    private int position_one;
    private int position_two;
    
    private Resources resources; 
	private ImageView iBack;
	private TextView tTitle;
	private View subTaView =null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        SystemBarTintManager.setContentView(this, R.layout.activity_my_order,
            getResources().getColor(R.color.firebrick));
//        setContentView(R.layout.activity_my_order);
        resources = getResources();
        
        subTaView = (View)findViewById(R.id.layoutSubTab);
        
        final ActionBar actionBar = getActionBar();  
        if (actionBar!=null)
        {
	        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);  
	        actionBar.setCustomView(R.layout.actionbar);//�Զ���ActionBar����  
        
            tTitle = (TextView)findViewById(R.id.actionbar_title);
	        iBack = (ImageView)findViewById(R.id.left_btn);
	        
	        tTitle.setText(resources.getText(R.string.my_order));
	        
	        iBack.setOnClickListener(new OnClickListener()
	        {	
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
	         	    finish();
				}
	        	
	        });	        
	        
        }
        InitWidth();        
        InitViewPager();
        InitTextView();
    }

    private void InitTextView() {
        tvTab1 = (TextView) findViewById(R.id.tv_tab_1);
        tvTab2 = (TextView) findViewById(R.id.tv_tab_2);
        tvTab3 = (TextView) findViewById(R.id.tv_tab_3);
        
        tvSubTab1 = (TextView) findViewById(R.id.tv_subtab_1);
        tvSubTab2 = (TextView) findViewById(R.id.tv_subtab_2);
        tvSubTab3 = (TextView) findViewById(R.id.tv_subtab_3);
        tvSubTab4 = (TextView) findViewById(R.id.tv_subtab_4);
        

        tvTab1.setOnClickListener(new MyOnClickListener(0));
        tvSubTab1.setOnClickListener(new MyOnClickListener(0));
        tvSubTab2.setOnClickListener(new MyOnClickListener(1));
        tvSubTab3.setOnClickListener(new MyOnClickListener(2));
        tvSubTab4.setOnClickListener(new MyOnClickListener(3));
        tvTab2.setOnClickListener(new MyOnClickListener(4));
        tvTab3.setOnClickListener(new MyOnClickListener(5));
    }

    private void InitViewPager() {
        mPager = (ViewPager) findViewById(R.id.vPager);
        fragmentsList = new ArrayList<Fragment>();                
       
        String userId="";
        
        AVUser user=   AVUser.getCurrentUser();
        if(user!=null)
        {
            userId=user.getObjectId();
        }
        
        Fragment fragment1 = OrdersListFragment.newInstance(OrdersListFragment.ORDER_TYPE_UNSEND,userId);
        Fragment fragment2= OrdersListFragment.newInstance(OrdersListFragment.ORDER_TYPE_UNPAY,userId);
        Fragment fragment3= OrdersListFragment.newInstance(OrdersListFragment.ORDER_TYPE_UNRECEIVE,userId);
        Fragment fragment4= OrdersListFragment.newInstance(OrdersListFragment.ORDER_TYPE_REFUNDING,userId);
        Fragment fragment5 = OrdersListFragment.newInstance(OrdersListFragment.ORDER_TYPE_FINISHED,userId);
        Fragment fragment6= OrdersListFragment.newInstance(OrdersListFragment.ORDER_TYPE_CLOSED,userId);
        
        fragmentsList.add(fragment1);
        fragmentsList.add(fragment2);
        fragmentsList.add(fragment3);
        fragmentsList.add(fragment4);
        fragmentsList.add(fragment5);
        fragmentsList.add(fragment6);
        
        mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    private void InitWidth() {
        ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
        bottomLineWidth = ivBottomLine.getLayoutParams().width;
        Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        offset = (int) ((screenW / 3.0 - bottomLineWidth) / 2);
        Log.i("MyStoreActivity", "offset=" + offset);

        position_one = (int) (screenW / 3.0);
        position_two = position_one * 2;
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    };

    public class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = null;
            switch (arg0) {
            case 0:
            case 1:
            case 2:
            case 3:
                if (currIndex == 4) {
                    animation = new TranslateAnimation(position_one, 0, 0, 0);
                    tvTab2.setTextColor(resources.getColor(R.color.black));
                } else if (currIndex == 5) {
                    animation = new TranslateAnimation(position_two, 0, 0, 0);
                    tvTab3.setTextColor(resources.getColor(R.color.black));
                }else {
                	animation = new TranslateAnimation(0, 0, 0, 0);
				}
                tvSubTab1.setTextColor(resources.getColor(R.color.black));
                tvSubTab2.setTextColor(resources.getColor(R.color.black));
                tvSubTab3.setTextColor(resources.getColor(R.color.black));
                tvSubTab4.setTextColor(resources.getColor(R.color.black));
                tvTab1.setTextColor(resources.getColor(R.color.red));
                if (arg0==0){
                	tvSubTab1.setTextColor(resources.getColor(R.color.red));
                }else if (arg0==1){
                	tvSubTab2.setTextColor(resources.getColor(R.color.red));
                }else if (arg0==2){
                	tvSubTab3.setTextColor(resources.getColor(R.color.red));
                }else if (arg0==3){
                	tvSubTab4.setTextColor(resources.getColor(R.color.red));
                }
                	
                subTaView.setVisibility(View.VISIBLE);
                break;
            case 4:
                if (currIndex == 0||currIndex==1||currIndex==2||currIndex==3) {
                    animation = new TranslateAnimation(0, position_one, 0, 0);
                    tvTab1.setTextColor(resources.getColor(R.color.black));
                } else if (currIndex == 5) {
                    animation = new TranslateAnimation(position_two, position_one, 0, 0);
                    tvTab3.setTextColor(resources.getColor(R.color.black));
                } 
                tvTab2.setTextColor(resources.getColor(R.color.red));
                subTaView.setVisibility(View.GONE);
                break;
            case 5:
                if (currIndex == 0||currIndex==1||currIndex==2||currIndex==3) {
                    animation = new TranslateAnimation(0, position_two, 0, 0);
                    tvTab1.setTextColor(resources.getColor(R.color.black));
                } else if (currIndex == 4) {
                    animation = new TranslateAnimation(position_one, position_two, 0, 0);
                    tvTab2.setTextColor(resources.getColor(R.color.black));
                }
                tvTab3.setTextColor(resources.getColor(R.color.red));
                subTaView.setVisibility(View.GONE);
                break;
            }
            
            currIndex = arg0;
            animation.setFillAfter(true);
            animation.setDuration(300);
            ivBottomLine.startAnimation(animation);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

	@Override
	public void onRefresh(PullToRefreshBase<ListView> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void goBack(View v)
	{
	    finish();
	}
}
